package zhang.lao.console.service;/**
 * Created by zhangqin on 2017/8/31.
 */

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhang.lao.build.mybatis.jdbc.auto.model.WnFarm;
import zhang.lao.build.mybatis.jdbc.auto.model.WnFarmExample;
import zhang.lao.build.mybatis.jdbc.auto.model.WnUserFarm;
import zhang.lao.build.mybatis.jdbc.auto.model.WnUserFarmExample;
import zhang.lao.build.tool.CookieKit;
import zhang.lao.build.tool.des.Des;
import zhang.lao.console.constant.ConsoleUserConstant;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnUserFarmDao;
import zhang.lao.extents.spring.exception.ConsoleException;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.login.LoginUserModel;
import zhang.lao.pojo.console.resp.HttpResultEnum;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**构建共有类
 * @author
 * @create 2017-08-31 17:56
 **/
@Service
public class BuildConsoleContentService {
    @Autowired
    private WnFarmDao wnFarmDao;
    @Autowired
    private WnUserFarmDao wnUserFarmDao;


    public ConsoleContext build(HttpServletRequest servletRequest) throws Exception{
        LoginUserModel loginUserModel = (LoginUserModel) servletRequest.getSession().getAttribute(ConsoleUserConstant.SESSION_USER);
        ConsoleContext consoleContext = new ConsoleContext();
        consoleContext.setLoginUserModel(loginUserModel);
        Cookie client1 = CookieKit.getCookieByName(servletRequest, "client");
        if(client1==null){
            consoleContext.setClient("pc");
        }else{
            consoleContext.setClient(client1.getValue());
        }

        if(loginUserModel.getUser_type()==1){
            Cookie cookie = CookieKit.getCookieByName(servletRequest, ConsoleUserConstant.COOKIE_COMPANY);
            if(cookie!=null){
                consoleContext.setCompanyId(Des.decodeValue(ConsoleUserConstant.COOKIE_Company_DES_KEY,cookie.getValue()));
            }else{
                throw new ConsoleException(HttpResultEnum.NO_CHOOSE_COMPANY);
            }
            List<String> farms = Lists.newArrayList();
            WnFarmExample wnFarmExample = new WnFarmExample();
            wnFarmExample.createCriteria().andCompanyIdEqualTo(consoleContext.getCompanyId());
            List<WnFarm> wnFarms = wnFarmDao.selectByExample(wnFarmExample);
            if(wnFarms!=null){
                for (WnFarm wnFarm : wnFarms) {
                    farms.add(wnFarm.getId());
                }
            }
            consoleContext.setFarms(farms);
        }else{
            WnUserFarmExample wnUserFarmExample = new WnUserFarmExample();
            wnUserFarmExample.createCriteria().andUserIdEqualTo(loginUserModel.getUser_id());
            List<WnUserFarm> wnUserFarms = wnUserFarmDao.selectByExample(wnUserFarmExample);
            List<String> farms = Lists.newArrayList();
            if(wnUserFarms!=null){

                for (WnUserFarm wnUserFarm : wnUserFarms) {
                    farms.add(wnUserFarm.getFarmId());
                }
            }
            consoleContext.setFarms(farms);
            consoleContext.setCompanyId(loginUserModel.getCompanyId());
        }
        return consoleContext;
    }
}

